package top.oylan.manager.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.*;
import top.oylan.manager.service.ActionService;
import top.oylan.manager.service.ProductService;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.Result;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by A on 2019/2/2.
 */
@Controller
@RequestMapping("/admin")
public class AdminHomeController {

    @Resource
    ActionService actionService;

    //  后台主页
    @RequestMapping(value = {"","/","/index"})
    public String index(){

        return "admin/index";
    }

    /**
     * 后台欢迎页
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/welcome",method = RequestMethod.GET)
    public String welcome(ModelMap modelMap){

        return "admin/home/welcome";
    }

    /**
     * 系统设置
     * @return
     */
    @RequestMapping(value = "/settings",method = RequestMethod.GET)
    public String settings(ModelMap modelMap){

        return "admin/home/settings";
    }

    /**
     *  后台菜单
     * @param id
     * @return
     */
    @RequestMapping("/menus")
    @ResponseBody
    public JSONObject menus(@RequestParam(value = "id",defaultValue = "0")Integer id){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        JSONObject object = new JSONObject();
        List<Action> list = actionService.selectActionsByParentIdAndUserId(id,user.getId());
        object.put("list",list);
        return object;
    }

    @Resource
    ProductService productService;

    /**
     * 首页推荐
     * @return
     */
    @RequestMapping("/recommend")
    public String recommend(){

        return "admin/home/recommend";
    }

    @RequestMapping("/recommend/product")
    public String product(@RequestParam("type")int type,
                          @RequestParam(value = "page" ,defaultValue = "1")int page,
                          ModelMap model){

        Pager pager = new Pager();
        pager.setCurrentPage(page);
        pager.setEveyPageRow(8);
        int count =  productService.countByType(type);
        pager.setTotalRow(count);
        List<Recommended> list =  productService.selectRecommendByType(type,pager);
        pager.setList(list);
        model.addAttribute("pager",pager);
        model.addAttribute("productType",type);
        return "admin/home/product";
    }

    @RequestMapping("/recommend/add")
    public String add(@RequestParam("type")int type){

        return "admin/home/add";
    }

    @RequestMapping(value = "/recommend/save",method = RequestMethod.POST)
    @ResponseBody
    public Result saveRecommend(@RequestBody JSONObject param){
        Result result = new Result();
        Byte type = param.getByte("type");
        List<Integer> ids = JSONArray.parseArray(JSONObject.toJSONString(param.getJSONArray("ids")), Integer.class);
        int res = productService.setRecommend(type,ids);
        if(res == 0){
            result.setCode((byte) 1);
            result.setMessage("设置失败");
        }
        return result;
    }

    @RequestMapping(value = "/recommend/cancel",method = RequestMethod.DELETE)
    @ResponseBody
    public Result cancel(@RequestParam("id")int id){
        Result result = new Result();
        int res = productService.deleteRecommend(id);
        if(res == 0){
            result.setCode((byte) 1);
            result.setMessage("取消失败");
        }
        return result;
    }
}
